package com.tjl.freme.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.tjl.frame.entity.Person;


@Controller("MyNewControler")
@RequestMapping(value = "/test")
public class MyNewControler {
	@RequestMapping("/test.do")
	public ModelAndView ShowList() {
		return new ModelAndView("list");
	}
	@RequestMapping("/slist.do")
	public String Slist() {
		return "list";
	}
	
	/**
	   * 方法的返回值采用ModelAndView， new ModelAndView("index", map);，
	   * 相当于把结果数据放到request里面
	   * @return
	   * @throws Exception
	   */
	  @RequestMapping("/toPerson4.do")
	  public ModelAndView toPerson4() throws Exception{
	    Person person = new Person();
	    person.setName("jerome");
	    person.setAge(22);
	    person.setAddress("nanan");
	    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	    Date date = format.parse("2012-12-21");
	    person.setBirthday(date);
	    
	    Map<String,Object> map = new HashMap<String, Object>();
	    map.put("p", person);
	    return new ModelAndView("index",map);
	  }
	  
	  /**
	   * 直接在方法的参数列表中来定义Map，这个Map即使ModelAndView里面的Map
	   * 由视图解析器统一处理，统一走ModelAndView的接口
	   * 也不建议使用
	   * @param map
	   * @return
	   * @throws Exception
	   */
	  @RequestMapping("/toPerson5.do")
	  public String toPerson5(Map<String,Object> map) throws Exception{
	    Person person = new Person();
	    person.setName("jerome");
	    person.setAge(22);
	    person.setAddress("nanan");
	    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	    Date date = format.parse("2012-12-21");
	    person.setBirthday(date);
	    
	    map.put("p", person);
	    return "index";
	  }
	  /**
	   *在参数列表中直接定义Model，model.addAttribute("p", person);
	   *把参数值放到request类里面去，建议使用
	   * @param map
	   * @return
	   * @throws Exception
	   */
	  @RequestMapping("/toPerson6.do")
	  public String toPerson6(Model model) throws Exception {
	    Person person = new Person();
	    person.setName("jerome");
	    person.setAge(22);
	    person.setAddress("nanan");
	    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	    Date date = format.parse("2012-12-21");
	    person.setBirthday(date);
	    //把参数值放到request类里面去
	    model.addAttribute("p", person);
	    return "jsp/index";
	  }
	  
	  /**
	   * ajax的请求返回值类型应该是void，参数列表里直接定义HttpServletResponse，
	   * 获得ProntWriter的类，最后可把结果写到页面
	   * 不建议使用
	   * @param name
	   * @param response
	   */
	  @RequestMapping("/ajax.do")
	  public void ajax(@RequestParam("name") String name, HttpServletResponse response) {
	    String result = "hello " + name;
	    try {
	      response.getWriter().write(result);
	    } catch (IOException e) {
	      e.printStackTrace();
	    }
	  }
	  /***********************************************
	   * 转发
	   * @return
	   */
	  @RequestMapping("/toAjax.do")
	  public String toAjax() {
	    return "ajax";
	  }
	  
	  /**
	   * 直接在参数的列表上定义PrintWriter,out.wrote(result);
	   * 把结果写到页面，建议使用
	   * @param name
	   * @param out
	   */
	  @RequestMapping("/ajax1.do")
	  public void ajax1(String name, PrintWriter out) {
	    String result="hello1 "+name;
	    out.write(result);
	  }
	  
	  @RequestMapping("/test1.do")

	    public ModelAndView test1(ModelAndView view) {

	        view.setViewName("redirect:index");

	        return view;

	    }

	 

	    @RequestMapping("/test2.do")

	    public ModelAndView test2(ModelAndView view) {

	        view.setViewName("redirect:login");

	        return view;

	    }

	    

	    @RequestMapping("/test3.do")

	    public ModelAndView test3(ModelAndView view) {

	        view.setViewName("redirect:/index");

	        return view;

	    }

	 

	    @RequestMapping("/test4.do")

	    public ModelAndView test4(ModelAndView view) {

	        view.setView(new RedirectView("/index", false));

	        return view;

	    }

	 

	    @RequestMapping("/test5.do")

	    public ModelAndView test5(ModelAndView view) {

	        view.setView(new RedirectView("index", false));

	        return view;

	    }

	 

	    @RequestMapping("/test6/{id}.do")

	    public ModelAndView test6(ModelAndView view, @PathVariable("id") int id) {

	        view.setViewName("redirect:/index{id}");
	        view.addObject("test", "test");

	        return view;

	    }

	    

	    @RequestMapping("/test7/{id}.do")

	    public ModelAndView test7(ModelAndView view, @PathVariable("id") int id) {

	        RedirectView redirectView = new RedirectView("/index{id}");

	        redirectView.setExpandUriTemplateVariables(false);

	        redirectView.setExposeModelAttributes(false);

	        view.setView(redirectView);

	        view.addObject("test", "test");

	        return view;

	    }

	  

	
}
